package com.fishv.fishvdianping.controller;

import com.fishv.fishvdianping.common.ErrorCode;
import com.fishv.fishvdianping.common.Result;
import com.fishv.fishvdianping.model.entity.UserRequest;
import com.fishv.fishvdianping.service.UserService;
import com.fishv.fishvdianping.utils.RegexUtils;
import com.fishv.fishvdianping.utils.ThrowUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author yuwei
 * @date 17:53 2024/10/20
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 发送验证码
     * @param phone
     * @return
     */
    @GetMapping("/code")
  public Result getCode(String phone){

        ThrowUtils.throwIf(RegexUtils.isPhoneInvalid(phone), ErrorCode.PARAMS_ERROR,"手机号格式错误");
        return Result.ok(userService.getCode(phone));

    }

    /**
     * 输入手机号和验证码登录
     * @param userRequest
     * @return
     */
    @PostMapping("/login")
    public Result login(@RequestBody UserRequest userRequest){
        ThrowUtils.throwIf(RegexUtils.isPhoneInvalid(userRequest.getPhone()), ErrorCode.PARAMS_ERROR,"手机号格式错误");
        ThrowUtils.throwIf(RegexUtils.isCodeInvalid(userRequest.getCode()), ErrorCode.PARAMS_ERROR,"验证码格式错误");
        return Result.ok(userService.login(userRequest));
    }
}
